package she_ji_mo_shi.dan_li.impl;

import she_ji_mo_shi.dan_li.LoadBalancer;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class HungrySingletonLoadBalancer implements LoadBalancer {
    private static final LoadBalancer INSTANCE = new HungrySingletonLoadBalancer();
    private List<String> serverList;
    private Random random;
    public static LoadBalancer getLoadBalancerInstance() {
        return INSTANCE;
    }
    private HungrySingletonLoadBalancer() {
        serverList = new ArrayList<>();
        random = new Random();
    };
    @Override
    public void addServer(String server) {
        serverList.add(server);
    }

    @Override
    public void removeServer(String server) {
        serverList.remove(server);
    }

    @Override
    public String getServer() {
        int i = random.nextInt(serverList.size());
        return serverList.get(i);
    }
}
